//
//  HMStatusOriginalView.swift
//  Weibo
//
//  Created by 黄亦寒 on 2016/9/24.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
import YYText
//原创微博
class HMStatusOriginalView: UIView {

    //记录原创微博的底部约束
    var OriginalViewBottomConstraint:Constraint?
    
    //给原创微博的子控件绑定数据
    var statusViewModel:HMStatusViewModel? {
        didSet {
            //昵称赋值
            screenNameLabel.text = statusViewModel?.status.user?.screen_name
            if let userImageURL = statusViewModel?.status.user?.profile_image_url {
                //头像赋值
                userImageView.sd_setImage(with: URL(string:userImageURL), placeholderImage: UIImage(named:"avatar_default_big"))
            }
            //时间赋值
            timeLabel.text = statusViewModel?.timeContent
            //内容赋值
            //contentLabel.text = statusViewModel?.status.text
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            //等级图片赋值
            mbrankImageView.image = statusViewModel?.mbrankImage
            //来源的文字内容
            sourceLabel.text = statusViewModel?.sourceContent
            //认证类型等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            
            //去除原创微博底部约束，再根据有无配图更新约束
            OriginalViewBottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status.pic_urls,picUrls.count > 0 {
                //代表有配图，配图不隐藏，更新转发微博的底部约束为配图的底部约束加间距,然后绑定数据
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    OriginalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
                })
                
                pictureView.picUrls = picUrls
            }else{
                //代表没有配图，配图隐藏，更新转发微博的底部约束为contentLabel的底部约束加间距
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    OriginalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(10).constraint
                })
            }
        }
        
        
        
    }
    
    //懒加载控件
    //用户头像
    private lazy var userImageView:UIImageView = UIImageView(image:UIImage(named:"avatar_default_big"))
      
    //认证类型等级
    private lazy var verifiedTypeImageView:UIImageView = UIImageView(image:UIImage(named:"avatar_vip"))

    private lazy var screenNameLabel:UILabel = UILabel(textColor:UIColor.darkGray,fontSize:14)
    //会员等级图片
    private lazy var mbrankImageView:UIImageView = UIImageView(image:UIImage(named:"common_icon_membership"))

    //时间
    private lazy var timeLabel:UILabel = UILabel(textColor:UIColor.darkGray,fontSize:14)

    //来源
    private lazy var sourceLabel:UILabel = UILabel(textColor:UIColor.darkGray,fontSize:14)
    
    //微博内容
    private lazy var contentLabel:YYLabel = {

//        let label = UILabel(textColor:UIColor.darkGray,fontSize:14)

        let label = YYLabel()
        //设置最大显示宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 20
        label.numberOfLines = 0
        return label
    }()
    
    //配图
    private lazy var pictureView:HMStatusPictureView = {
        let view = HMStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局控件
    func setUpUI() {
        backgroundColor = UIColor.white
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        userImageView.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(10)
            make.top.equalTo(self).offset(10)
            make.width.equalTo(35)
            make.height.equalTo(35)
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_right)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.left.equalTo(userImageView.snp_right).offset(10)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.left.equalTo(screenNameLabel.snp_right).offset(10)

        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.left.equalTo(screenNameLabel)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(10)
            make.right.equalTo(self).offset(-10)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
        }
        
        //设置原创微博底部的约束为微博内容往下为10的间隔
        self.snp_makeConstraints { (make) in
            OriginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
        }
        
        //设置点击高亮文字执行的闭包
        contentLabel.highlightTapAction = { (containerView,attributedString,range,rect) in
            //根据匹配的范围获取对应的字符串
            let text = (attributedString.string as NSString).substring(with: range)
            if text.hasPrefix("http") {
                print("点击链接")
            }
            print(text)
            
            
        }
    }
    
    
    
}
